# -- coding: utf-8 --
from datetime import datetime

'''
### 可迭代对象
能够被 for … in 循环遍历的对象被称为可迭代对象 iterable，列表、元组、集合均属于可迭代对象。
'''
# list = ['afc','123','341']
# for i in list:
#     print(i)
#
#
# '''
# 这样可遍历字典内的所有值
# '''
# dict ={'name':"gaou",'age':'18','address':'guizi'}
# for a in dict:
#     print(a)
#     print(dict[a])
#
# print(datetime.now())
# c = datetime.now()
# e = datetime.now()
#
# f= e-c
# print(f.microseconds)

'''
循环遍历列表中的数据，如果在集合内就会报错
'''
# list = ['123','qwe','rty']
# i = 0
# while i < len(list):
#     strings = list[i]
#     print(strings)
#     i = i+ 1

'''
## 迭代器
迭代器 iterator 是一个特殊的对象，用于遍历访问可迭代对象 iterable。

最下面的就是第一种方法的原始实现方法
'''

# list = ['www', 'imooc', 'com']
# for item in list:
#     print(item)



# list = ['www', 'imooc', 'com']
# listIterator = iter(list)
# while True:
#     try:
#         item = next(listIterator)
#         print(item)
#     except StopIteration:
#         break
#
#
#
#
#
# list = ['www', 'imooc', 'com']
# itera =iter(list)
# while True:
#     try:
#         item = next(itera)
#         print(item)
#     except StopIteration:
#         break


# file = open(r"test.txt")
# count = 0
#
# while True:
#     line = file.readline()
#     if not line:
#         break
#     words = line.split()
#     for word in words:
#         print(word)
#
#         count= count+1
#
# print(count)




# file = open('test.txt')
# count = 0
#
# while True:
#     line = file.readline()
#     if not line:
#         break
#
#     words = line.split()
#     for word in words:
#         print(word)
#         count = count + 1
# print('count = %d' % count)


# file = open('test.txt')
# dict = {}
#
# while True:
#     line = file.readline()
#     if not line:
#         break
#
#     words = line.split()
#     for word in words:
#         if word in dict:
#             dict[word] +=1 # 这样才是+1
#         else:
#             dict[word] =1
#
# for word,count in dict.items():
#     print("word: %s : %d" % (word,count))

'''
上面两个，程序中存在明显的代码重复。
下面使用迭代器的方法

下面实现类 IterateWord 用于简化遍历文本中的单词，**类 IterateWord 既是可迭代对象也是迭代器: **

类 IterateWord 是可迭代对象，提供了 __iter__ 方法，返回一个迭代器
类 IterateWord 是迭代器，提供了 __next__ 方法，返回下一个遍历的对象
'''

class IterateWord:
    def __init__(self,file):
        self.file = file
        self.words = []

    def __iter__(self):
        return self

    def __next__(self):
        if len(self.words) ==0:
            self.get_non_blank_line()
        word = self.words.pop(0)
        return word

    def get_non_blank_line(self):
        while True:
            line =file.readline()
            if not line:
                raise StopIteration
            self.words = line.split()
            if len(self.words) != 0:
                break



file = open(r"test.txt")
# count = 0
#
# for word in IterateWord(file):
#     print(word)
#     count = count +1

dict = {}

for word in IterateWord(file):
    if word in dict:
        dict[word] +=1
    else:
        dict[word] =1

for word,count in dict.items():
    print("word: %s : %d" % (word, count))


'''
感觉 这种封装的迭代器方法要比普通的方法麻烦的多，本身就可以实现何必要在自己写方法呢，但为了更深入的理解还是要这样做，虽然我现在还是没有搞懂(苦笑hhhh)
'''













